package com.qfedu.buy.server.service.impl;

import com.qfedu.buy.common.config.RedisKeyConfig;
import com.qfedu.buy.common.config.SystemConfig;
import com.qfedu.buy.common.dto.GoodsSkuDetailDto;
import com.qfedu.buy.common.dto.OrderCartDto;
import com.qfedu.buy.common.dto.OrderGoodsDto;
import com.qfedu.buy.common.dto.PayDto;
import com.qfedu.buy.common.util.IdGenerator;
import com.qfedu.buy.common.util.RedissonUtil;
import com.qfedu.buy.common.vo.R;
import com.qfedu.buy.server.dao.OrderDao;
import com.qfedu.buy.server.dao.OrderItemDao;
import com.qfedu.buy.server.entity.Order;
import com.qfedu.buy.server.entity.OrderItem;
import com.qfedu.buy.server.provider.GoodsProvider;
import com.qfedu.buy.server.provider.PayProvider;
import com.qfedu.buy.server.service.intf.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: NeighborBuy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-02-04 09:14
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao dao;
    @Autowired
    private OrderItemDao itemDao;
    @Autowired
    private GoodsProvider goodsProvider;//调用商品服务
    @Autowired
    private PayProvider payProvider;
    @Override
    @GlobalTransactional(name = "llgorder-saveorder") //开启全局事务 分布式事务
    public R saveOrder(OrderGoodsDto dto, int uid) {
        //查询商品信息
        GoodsSkuDetailDto detailDto=goodsProvider.queryById(dto.getGskuid());
        if(detailDto!=null){
            //使用分布式锁， key
            String key=RedisKeyConfig.ORDER_GSKU+detailDto.getGid()+":"+dto.getGskuid();
            RLock rLock=RedissonUtil.getLock(key,RedisKeyConfig.ORDER_GSKU_TIME);
            if(rLock.isLocked()) {
                try{
                    //商品有效
                    if (detailDto.getStore() > dto.getNum()) {
                        //库存够
                        //生成订单
                        Order order = new Order();
                        order.setUid(uid);
                        //分布式唯一 雪花算法
                        order.setOno(IdGenerator.getInstance().nextId()+"");
                        order.setFlag(SystemConfig.ORDER_STATUS_NOPAY);
                        order.setTotalmoney(detailDto.getGprice()*dto.getNum());
                        //优惠劵服务 省略
                        //积分抵扣-用户服务 省略
                        order.setFreemoney(0);
                        order.setPaymoney(order.getTotalmoney()-order.getFreemoney());
                        order.setPaytype(dto.getPaytype());
                        order.setUaid(dto.getUaid());
                        if(dao.insert(order)>0){
                            //库存扣减-商品服务
                            goodsProvider.change(detailDto.getGskuid(),-dto.getNum());
                            //生成订单详情
                            OrderItem orderItem=new OrderItem(order.getId(),
                                    detailDto.getGid(),detailDto.getGskuid(),detailDto.getGprice()
                                    ,dto.getNum(),detailDto.getGtitle(),detailDto.getGsku(),
                                    detailDto.getGpicurl()
                            );
                            itemDao.insert(orderItem);
                            //确认订单的时候，选择了支付方式  需要调用支付服务 生成预支付信息
                            R r=payProvider.create(new PayDto(order.getOno(),detailDto.getGtitle(),
                                    order.getPaymoney(),order.getPaytype()));
                            return r;
                        }
                    }
                }
                finally {
                    //释放锁
                    rLock.unlock();
                }

            }else {
                return R.fail("资源竞争激烈，获取锁失败");
            }
        }
        return R.fail();
    }

    @Override
    public R saveOrder(OrderCartDto dto, int uid) {
        return null;
    }
}
